<?php
namespace App\Http\Controllers;
use Cookie;
use Illuminate\Http\Request;
use DB;
use Str;


class AdminController extends Controller
{
    //后台登录页面渲染
    public function login()
    {
        return view('admin.login'   );
    }

    //后台登录页面1渲染
    public function login1()
    {
        return view('admin.page.login-1');
    }

    //后台登录页面2渲染
    public function login2()
    {
        return view('admin.page.login-2');
    }

    //后台登录页面3渲染
    public function login3()
    {
        return view('admin.page.login-3');
    }

    //后台主页页面渲染
    public function admin()
    {// dump($_COOKIE['adminuser']);
        $r = DB::table('adminuser') -> where('admin_name',Cookie::get('adminuser')) -> first();
        if(empty(Cookie::get('adminuser')) || $r == null){
           return redirect('/admin/login');
        };

        return view('admin.index');
    }

    //后台主页一页面渲染
    public function welcome1()
    {
        //统计用户数量
        $DB = DB::table('user');
        $c = $DB -> get();
        $usercount = count($c);
        //统计房屋数量
        $hcount = DB::table('house') -> get();
        $housecount = count($hcount);
        // //浏览次数
        //     @session_start();
        //     $counter = intval(file_get_contents("counter.dat"));
        //     if(!$_SESSION['user'])
        //     {
        //      $_SESSION['user'] = true;
        //      $counter++;
        //      $fp = fopen("counter.dat","w");
        //      fwrite($fp, $counter);
        //      fclose($fp);
        //     }
        //统计成交房屋数量
        // 这里写统计房屋成交的数量计算和
        return view('admin.page.welcome-1',['usercount' => $usercount,'housecount' => $housecount]);
    }

    //后台主页三页面渲染
    public function welcome3()
    {
        //统计用户数量
        $DB = DB::table('user');
        $c = $DB -> get();
        $usercount = count($c);
        //统计房屋数量
        $hcount = DB::table('house') -> get();
        $housecount = count($hcount);
        // //浏览次数
        //     @session_start();
        //     $counter = intval(file_get_contents("counter.dat"));
        //     if(!$_SESSION['user'])
        //     {
        //      $_SESSION['user'] = true;
        //      $counter++;
        //      $fp = fopen("counter.dat","w");
        //      fwrite($fp, $counter);
        //      fclose($fp);
        //     }
        //统计成交房屋数量
        // 这里写统计房屋成交的数量计算和
        return view('admin.page.welcome-3',['usercount' => $usercount,'housecount' => $housecount]);
    }
    //文件上传渲染
    public function upload(){return view('admin.page.upload');}
    //图标列表
    public function icon(){return view('admin.page.icon');}
    //图标选择
    public function icon_picker(){return view('admin.page.icon-picker');}
    //颜色选择
    public function color_select(){return view('admin.page.color-select');}
    //下拉选择
    public function table_select(){return view('admin.page.table-select');}
    //富文本编辑器
    public function editor(){return view('admin.page.editor');}
    //省市区选择器
    public function area(){return view('admin.page.area');}
    //表单示例
    public function form(){return view('admin.page.form');}
    //失效页面
    public function shi(){return view('admin.page.404');}
    //菜单管理
    public function menu(){return view('admin.page.menu');}

    //租房管理页面渲染
    public function rent(Request $request)
    {
        $key = $request -> key;
        $row = DB::table('house')
        ->where('zh_tp',2)
        ->Where('zh_name', 'like', '%'.$key.'%')
        ->join('rent_housetype','house.zh_house_class','=','rent_housetype.rid')
        ->get();

        return view('admin.page.rent',['row' => $row,'key' => $key]);
    }

    //二手房管理页面渲染
    public function secondhouse(Request $request)
    {
        $key = $request -> key;
        $row = DB::table('house')
        ->where('zh_tp',1)
        ->Where('zh_name', 'like', '%'.$key.'%')
        ->join('rent_housetype','house.zh_house_class','=','rent_housetype.rid')
        ->get();
        return view('admin.page.secondhouse',['row' => $row,'key' => $key]);
    }



    //消遣页面渲染
    public function zykp(){return view('admin.zykp');}
    //坦克页面渲染
    public function TankBattle(){return view('admin.TankBattle');}
    //按钮示列页面渲染
    public function button(){return view('/admin.page.button');}
    //弹出层示列页面渲染
    public function layer(){return view('admin.page.layer');}
    //二手房类型管理渲染
    public function sell_houseclass()
    {
        $row = DB::table('sell_housetype') -> get();
        return view('admin.page.sell_houseclass',['row' => $row]);
    }


    //租房类型管理渲染
    public function rent_houseclass()
    {
        $row = DB::table('rent_housetype') -> get();
        return view('admin.page.rent_houseclass',['row' => $row]);
    }

    //租房管理渲染
    public function add_adminuser_renting()
    {

        return view('admin.page.add_adminuser_renting');
    }

    //用户管理页面渲染
    public function user(Request $key)
    {
        $k = $key -> key;
        $row = DB::table('user')
            -> where('user_status', 'like', '%'.$k.'%')
            -> orWhere('user_name', 'like', '%'.$k.'%')
            -> orWhere('id', 'like', '%'.$k.'%')
            -> orWhere('user_tel', 'like', '%'.$k.'%')
            -> orWhere('user_truename', 'like', '%'.$k.'%')
            -> orWhere('user_email', 'like', '%'.$k.'%')
            -> orWhere('user_sex', 'like', '%'.$k.'%')
            -> orWhere('user_city', 'like', '%'.$k.'%')
            ->get();
            //->simplepaginate(2);
        return view('admin.page.user',['row' => $row,'key' => $k]);
    }


    //管理人员管理页面渲染
    public function adminuser(Request $key)
    {
        $k = $key -> key;
        $row =DB::table('adminuser')
            ->where('admin_name', 'like', '%'.$k.'%')
            ->orWhere('admin_truename','like','%'.$k.'%')
            ->orWhere('admin_email','like','%'.$k.'%')
            ->orWhere('admin_tel','like','%'.$k.'%')
            ->orWhere('admin_sex','like','%'.$k.'%')
            ->get();
        return view('admin.page.adminuser',['row' => $row,'key' => $k]);
    }

    //修改工作人员资料页面渲染
    public function admin_xiugai($id)
    {
        $post = DB::table('post') -> get();
        $row = DB::table('adminuser') -> where('id',Request() -> id) -> first();
        $company = DB::table('company') -> get();
        return view('admin.page.admin_xiugai',['row' => $row,'post' => $post,'company' => $company]);
    }

    //工作人员添加页面渲染
    public function add_adminuser()
    {
        $company = DB::table('company') -> get();
        $post = DB::table('post') -> get();
        return view('admin.page.add_adminuser',['post' => $post,'company' => $company]);
    }


    //二手房添加页面渲染
    public function add_adminuser_house()
    {   $post = DB::table('rent_housetype') -> get();



        return view('admin.page.add_adminuser_house',['post' => $post]);
    }

    //分公司管理页面渲染
    public function company(Request $key)
    {
        $k = $key -> key;
        $row =DB::table('company')
            ->where('company', 'like', '%'.$k.'%')
            ->orWhere('company_place','like','%'.$k.'%')
            ->orWhere('company_level','like','%'.$k.'%')
            ->orWhere('company_status','like','%'.$k.'%')
            ->orWhere('company_manager','like','%'.$k.'%')
            ->orWhere('company_managermin','like','%'.$k.'%')
            ->get();
        return view('admin.page.company',['row' => $row,'key' => $k]);
    }

    //添加分公司页面渲染
    public function add_company()
    {
        $people = DB::table('adminuser') -> where('admin_post','房产经纪人') -> orWhere('admin_post','文员') -> get();
        return view('admin.page.add_company',['pp' => $people]);
    }

    //二手房添加功能
    public function add_house(Request $request)
    {
        //图片上传
        $file = request()->file('zh_picture');
        $folder_name = "/index/img/" . date("Ym/d", time());
        $upload_path = public_path() . '/' . $folder_name;
        $extension  =  strtolower($file->getClientOriginalExtension())  ?:  'png';
        $filename =  time() . '_' . Str::random(10) . '.' . $extension;
        $user = $folder_name . '/' . $filename;
        $file->move($upload_path, $filename);
        $aid = Cookie::get('adminuser');
        //经纪人
        $id = DB::table('adminuser') -> where('admin_name',$aid) -> get('id');
        foreach($id as $idd){}
        //每平方单价
        $jg =($request ->zh_price.'0000') / ($request ->acreage);
        //首付
        $sf =($request ->zh_price.'0000') * ($request ->down_payment)/100;
        //月供
        $yg =(($request ->zh_price.'0000') - ($request ->zh_price.'0000') * ($request ->down_payment)/100)/$request ->installment/12*6.55/100+(($request ->zh_price.'0000') - ($request ->zh_price.'0000') * ($request ->down_payment)/100)/$request ->installment/12;

        if($file){
         DB::table('house')->insert([
                [
                    'zh_picture' => $user,
                    'zh_name' => $request -> zh_name,
                    'zh_square'  => $request -> zh_square,
                    'zh_place'  => $request -> zh_place,
                    'zh_house_class'=> $request -> zh_house_class,
                    'zh_price'=> $request -> zh_price,
                    'anym'=> $jg,
                    'house_type'=> $request -> house_type,
                    'acreage'=> $request -> acreage,
                    'down_payment'=> $sf,
                    'floor'=> $request -> floor,
                    'orientation'=> $request -> orientation,
                    'fitment_finish'=> $request -> fitment_finish,
                    'site'=> $request -> site,
                    'Room_evaluation'=> $request -> Room_evaluation,
                    'installment'=>$yg,
                    'installmenter'=> $request -> installment,
                    'zh_tp'  => 1,
                    'percentage' => $request -> down_payment,
                    'aid' => $idd -> id,
                ]
            ]);
        }

         return '成功'. @redirect('/admin/secondhouse');


    }
    //二手房删除
    public function add_house_del(Request $request)
    {
        $row = Request() ->id;

        DB::table('house') -> where('id',$row) -> delete();
        return redirect("/admin/secondhouse");
    }

    //二手房修改页面渲染
    public function add_house_xiugai()
    {
        $row = Request() ->id;
        $rows = DB::table('house') -> where('id',$row) -> first();
        $post = DB::table('rent_housetype') -> get();
        $house_class = DB::table('rent_housetype') -> where('rid',$rows -> zh_house_class) -> first();
        return view('admin.page.add_house_xiugai',['post' => $post,'rows' => $rows,'house_class' => $house_class]);
    }

    //二手房修改功能
    public function add_house_xiuer(Request $request)
    {
        //图片上传
        $file = request()->file('zh_picture');
        $folder_name = "/index/img/" . date("Ym/d", time());
        $upload_path = public_path() . '/' . $folder_name;
        $extension  =  strtolower($file->getClientOriginalExtension())  ?:  'png';
        $filename =  time() . '_' . Str::random(10) . '.' . $extension;
        $user = $folder_name . '/' . $filename;
        $file->move($upload_path, $filename);
        $aid = Cookie::get('adminuser');
        //经纪人
        $id = DB::table('adminuser') -> where('admin_name',$aid) -> get('id');
        foreach($id as $idd){}
        //每平方单价
        $jg =($request ->zh_price.'0000') / ($request ->acreage);
        //首付
        $sf =($request ->zh_price.'0000') * ($request ->down_payment)/100;
        //月供
        $yg =(($request ->zh_price.'0000') - ($request ->zh_price.'0000') * ($request ->down_payment)/100)/$request ->installment/12*6.55/100+(($request ->zh_price.'0000') - ($request ->zh_price.'0000') * ($request ->down_payment)/100)/$request ->installment/12;
        
        if($file){

            $data=array(
                    'zh_picture' => $user,
                    'zh_name' => $request -> zh_name,
                    'zh_square'  => $request -> zh_square,
                    'zh_place'  => $request -> zh_place,
                    'zh_house_class'=> $request -> zh_house_class,
                    'zh_price'=> $request -> zh_price,
                    'anym'=> $jg,
                    'house_type'=> $request -> house_type,
                    'acreage'=> $request -> acreage,
                    'down_payment'=> $sf,
                    'floor'=> $request -> floor,
                    'orientation'=> $request -> orientation,
                    'fitment_finish'=> $request -> fitment_finish,
                    'site'=> $request -> site,
                    'Room_evaluation'=> $request -> Room_evaluation,
                    'installment'=>$yg,
                    'installmenter'=> $request -> installment,
                    'zh_tp'  => 1,
                    'percentage' => $request -> down_payment,
                    'aid' => $idd -> id,
            );
           

             DB::table('house') -> where('id',$request -> id) -> update($data);

         
        }
        return '成功'. @redirect('/admin/secondhouse');
       
    }



    //添加租房类型页面渲染
    public function add_rentclass()
    {
        $post = DB::table('rent_housetype') -> get();
        return view('admin.page.add_rentclass',['post' => $post]);
    }

    //添加租房功能
    public function add_rentclass_er(Request $request)
    {
        //图片上传
        $file = request()->file('zh_picture');
        $folder_name = "/index/img/" . date("Ym/d", time());
        $upload_path = public_path() . '/' . $folder_name;
        $extension  =  strtolower($file->getClientOriginalExtension())  ?:  'png';
        $filename =  time() . '_' . Str::random(10) . '.' . $extension;
        $user = $folder_name . '/' . $filename;
        $file->move($upload_path, $filename);
        $aid = Cookie::get('adminuser');
        //经纪人
        $id = DB::table('adminuser') -> where('admin_name',$aid) -> get('id');
        foreach($id as $idd){}
        //每平方单价
        $jg =($request ->zh_price.'0000') / ($request ->acreage);
        //首付
        $sf =($request ->zh_price.'0000') * ($request ->down_payment)/100;
        //月供
        $yg =(($request ->zh_price.'0000') - ($request ->zh_price.'0000') * ($request ->down_payment)/100)/$request ->installment/12*6.55/100+(($request ->zh_price.'0000') - ($request ->zh_price.'0000') * ($request ->down_payment)/100)/$request ->installment/12;

        if($file){
         DB::table('house')->insert([
                [
                    'zh_picture' => $user,
                    'zh_name' => $request -> zh_name,
                    'zh_square'  => $request -> zh_square,
                    'zh_place'  => $request -> zh_place,
                    'zh_house_class'=> $request -> zh_house_class,
                    'zh_price'=> $request -> zh_price,
                    'anym'=> $jg,
                    'house_type'=> $request -> house_type,
                    'acreage'=> $request -> acreage,
                    'down_payment'=> $sf,
                    'floor'=> $request -> floor,
                    'orientation'=> $request -> orientation,
                    'fitment_finish'=> $request -> fitment_finish,
                    'site'=> $request -> site,
                    'Room_evaluation'=> $request -> Room_evaluation,
                    'installment'=>$yg,
                    'percentage' => $request -> down_payment,
                    'installmenter'=> $request -> installment,
                    'zh_tp'  => 2,
                    'aid' => $idd -> id,
                ]
            ]);
        }

         return '成功'. @redirect('/admin/rent');
    }
    
    //修改租房
    public function add_house_rent()
    {
        $row = Request() ->id;
        $rows = DB::table('house') -> where('id',$row) -> first();
        $post = DB::table('rent_housetype') -> get();
        $house_class = DB::table('rent_housetype') -> where('rid',$rows -> zh_house_class) -> first();
        return view('admin.page.add_house_rent',['post' => $post,'rows' => $rows,'house_class' => $house_class]);
    }

    //租房修改功能
    public function add_rent_rent(Request $request)
    {
        //图片上传
        $file = request()->file('zh_picture');
        $folder_name = "/index/img/" . date("Ym/d", time());
        $upload_path = public_path() . '/' . $folder_name;
        $extension  =  strtolower($file->getClientOriginalExtension())  ?:  'png';
        $filename =  time() . '_' . Str::random(10) . '.' . $extension;
        $user = $folder_name . '/' . $filename;
        $file->move($upload_path, $filename);
        $aid = Cookie::get('adminuser');
        //经纪人
        $id = DB::table('adminuser') -> where('admin_name',$aid) -> get('id');
        foreach($id as $idd){}
        //每平方单价
        $jg =($request ->zh_price.'0000') / ($request ->acreage);
        //首付
        $sf =($request ->zh_price.'0000') * ($request ->down_payment)/100;
        //月供
        $yg =(($request ->zh_price.'0000') - ($request ->zh_price.'0000') * ($request ->down_payment)/100)/$request ->installment/12*6.55/100+(($request ->zh_price.'0000') - ($request ->zh_price.'0000') * ($request ->down_payment)/100)/$request ->installment/12;
        
        if($file){

            $data=array(
                    'zh_picture' => $user,
                    'zh_name' => $request -> zh_name,
                    'zh_square'  => $request -> zh_square,
                    'zh_place'  => $request -> zh_place,
                    'zh_house_class'=> $request -> zh_house_class,
                    'zh_price'=> $request -> zh_price,
                    'anym'=> $jg,
                    'house_type'=> $request -> house_type,
                    'acreage'=> $request -> acreage,
                    'down_payment'=> $sf,
                    'floor'=> $request -> floor,
                    'orientation'=> $request -> orientation,
                    'fitment_finish'=> $request -> fitment_finish,
                    'site'=> $request -> site,
                    'Room_evaluation'=> $request -> Room_evaluation,
                    'installment'=>$yg,
                    'installmenter'=> $request -> installment,
                    'zh_tp'  => 2,
                    'percentage' => $request -> down_payment,
                    'aid' => $idd -> id,
            );
           

             DB::table('house') -> where('id',$request -> id) -> update($data);

         
        }
        return '成功'. @redirect('/admin/rent');
       
    }

    //租房删除
    public function rent_deler(Request $request)
    {
        $row = Request() ->id;

        DB::table('house') -> where('id',$row) -> delete();
        return redirect("/admin/rent");
    }
    //基本信息页面渲染
    public function infor()
    {
        $admin = DB::table('adminuser');
        $row = $admin -> where('admin_name',Cookie::get('adminuser')) -> first();
        return view('admin.page.infor',['row' => $row]);
    }

    //修改密码
    public function changepwd()
    {
        return view('admin.page.changepwd');
    }



    //后台登录功能
    public function loginin(Request $row)
    {
        if($_COOKIE['AdminCode'] == $row -> code){
        $ro = DB::table('adminuser') -> where('admin_name',$row -> login) -> where('admin_pwd',md5($row -> pwd)) -> where('admin_status','是') -> first();
        if($ro){
              return redirect("/admin/admin")->cookie('adminuser',$row -> login);
        }else{

               return '账户或密码错误'. @redirect('/admin/login');
         }
        }else{
               return '验证码错误'. @redirect('/admin/login');
    }
   }


   //停营分公司功能
   public function company_stop(Request $id)
   {
       $date = array('company_status' => '停运营');
       DB::table('company') -> where('id',$id -> id) -> update($date);
       return redirect('/admin/company');
   }

   //正在营业状态功能
   public function company_do(Request $id)
   {
       $date = array('company_status' => '正在营业');
       DB::table('company') -> where('id',$id -> id) -> update($date);
       return redirect('/admin/company');
   }

   //休息状态功能
   public function company_rest(Request $id)
   {
       $date = array('company_status' => '休息中');
       DB::table('company') -> where('id',$id -> id) -> update($date);
       return redirect('/admin/company');
   }

   public function adminuser_freeze()
   {
       $date = array('admin_status' => '否');
       DB::table('adminuser') -> where('id',$id -> id) -> update($date);
       return redirect('/admin/adminuser');
   }

    //修改工作人员资料功能
    public function admin_xiugaiin(Request $row)
    {
       $data=array(
           'admin_name' => $row -> admin_name,
           'admin_truename' => $row -> admin_truename,
           'admin_company'  => $row -> admin_company,
           'admin_tel'  => $row -> admin_tel,
           'admin_email'=> $row -> admin_email,
           'admin_post'=> $row -> admin_post,
           'admin_status'=> $row -> admin_status,
       );
       DB::table('adminuser') -> where('id',$row -> id) -> update($data);
        return redirect("/admin/adminuser");
    }

    //删除工作人员功能(已完成关联删除)
    public function adminuser_del(Request $row)
    {
        DB::table('manager') -> where('mid',$row -> id) -> delete();
        DB::table('adminuser') -> where('id',$row -> id) -> delete();
        DB::table('house') -> where('aid',$row -> id) -> delete();
        return redirect("/admin/adminuser");
    }

    //冻结用户
    public function user_del(Request $row)
    {
        $r = array('user_status' => '否','freezing_time' => date('Y-m-d h:m:s'),);
        DB::table('user') -> where('id',$row -> id) -> update($r);
        return redirect("/admin/user");
    }

    //解冻用户
    public function user_undel(Request $row)
    {
        $r = array('user_status' => '是',);
        DB::table('user') -> where('id',$row -> id) -> update($r);
        return redirect("/admin/user");
    }


    //添加工作人员功能(已完成关联添加与限制添加)
    public function add_adminuserin(Request $row)
    {
        if($row -> admin_pwd == $row -> admin_repwd)
        {
            //判定昵称不能一致
            $flase = DB::table('adminuser') -> where('admin_name',$row -> admin_name) -> first();
            if($flase !== null){
                return redirect('/admin/add_adminuser');
            }
            //身份证是否合法
            if(strlen($row -> admin_idcard) < 18)
             {
                    return redirect('/admin/add_adminuser');
             }
             if(substr($row->admin_idcard,6,2) != 20 && substr($row->admin_idcard,6,2) != 19)
             {
                  return redirect('/admin/add_adminuser');
             }
             //判断分公司只允许有一个总经理与副经理
             if($row -> admin_post == '副经理' || $row -> admin_post == '总经理')
             {
                 $res =  DB::table('adminuser') -> where('admin_post',$row -> admin_post) -> where('admin_company',$row -> admin_company) -> first();
                 if($res !== null){
                     //如若冲突则返回添加页面
                    return redirect('/admin/add_adminuser');
                 }
             }
            //电话号码是否合法
            if(strlen($row -> admin_tel) < 11)
            {
                 return redirect('/admin/add_adminuser');
            }
            if(substr($row->admin_tel,0,1) != 1)
            {
                 return redirect('/admin/add_adminuser');
            }
            $str = explode('@',$row -> admin_email);
            $r = empty($str[1]);
            //邮箱是否合法
            if($r == true)
            {
                return redirect('/admin/add_adminuser');
            }
            //获取现在年份
            $year = date('Y');
            //截取工作人员输入身份证的出生年份
            $age = substr($row -> admin_idcard,6,4);
            //获取工作人员年龄
            $admin_age = $year-$age;
            // dd($row -> admin_post);
            $data=array(
                'create_time' => date('Y-m-d h:m:s'),
                'admin_pwd' => md5($row -> admin_pwd),
                'admin_repwd' => md5($row -> admin_repwd),
                'admin_name' => $row -> admin_name,
                'admin_truename' => $row -> admin_truename,
                'admin_sex'  => $row -> admin_sex,
                'admin_tel'  => $row -> admin_tel,
                'admin_email'=> $row -> admin_email,
                'admin_status'=> $row -> admin_status,
                'admin_post'=> $row -> admin_post,
                'admin_idcard' => $row -> admin_idcard,
                'admin_age' => $admin_age,
                'admin_company' => $row -> admin_company,
            );
           $mid = DB::table('adminuser') -> insertGetid($data);

            if($row -> admin_post == '副经理' || $row -> admin_post == '总经理')
            {
                $da = array(
                    'manager_name' => $row -> admin_truename,
                    'manager_company' => $row -> admin_company,
                    'manager_idcard' => $row -> admin_idcard,
                    'manager_age' => $admin_age,
                    'manager_company' => $row -> admin_company,
                    'manager_post' => $row -> admin_post,
                    'manager_tel' => $row -> admin_tel,
                    'mid' => $mid,
                );
                DB::table('manager') -> insert($da);
                return redirect("/admin/adminuser");
            }
            return redirect("/admin/adminuser");
        }
        return redirect("/admin/add_adminuser");
    }

        //删除房屋类型功能
        public function rent_del(Request $id)
        {
            DB::table('rent_housetype') -> where('r_id',$id -> r_id) -> delete();
            DB::table('house') -> where('zh_house_class') ->delete();
            return redirect("/admin/rent_houseclass");
        }

        //添加租房类型功能
        public function add_rentclassin(Request $row)
        {
            $rent = DB::table('rent_housetype');
           $res = $rent -> where('house_type',Request() -> house_type) -> get();

               $data = array(
                   'house_type' => Request() -> house_type,
               );
               $wo = $rent -> insertGetid($data);
               $date = array(
                   'rid' => $wo,
               );
               $rent ->where('r_id',$wo) -> update($date);
               return redirect("/admin/rent_houseclass");
           }


        //后台退出功能
        public function login_out(){
            $cookie = Cookie::forget('adminuser');
            return redirect('/admin/login')->withCookie($cookie);
        }


        //基本信息修改功能
        public function inforin(Request $row)
        {
            $data=array(
                'admin_name' => $row -> admin_name,
                'admin_truename' => $row -> admin_truename,
                'admin_sex'  => $row -> admin_sex,
                'admin_tel'  => $row -> admin_tel,
                'admin_email'=> $row -> admin_email,
            );
            DB::table('adminuser') -> where('id',Request() -> id) -> update($data);
             return redirect("/admin/adminuser");
        }

        //修改密码功能
        public function changepwdin(Request $form)
        {
            //验证旧密码
            $row = DB::table("adminuser") -> where('admin_name',Cookie::get('adminuser')) -> first();
            if(md5($form -> oldpwd) == $row -> admin_pwd)
            {
                //验证form表单输入的新密码与旧密码是否相同
                if($form -> admin_pwd == $form -> oldpwd){
                    return redirect('/admin/changepwd').'新密码与旧密码不能相同';
                }
                //验证两次新密码输入是否相同
                if($form -> admin_pwd == $form -> admin_repwd){
                    $pwd = array(
                        'admin_pwd' => md5($form -> admin_pwd),
                        'admin_repwd' => md5($form -> admin_repwd),
                    );
                    DB::table('adminuser') -> where('id',$row -> id) -> update($pwd);
                     return redirect('/admin/adminuser');
                }else{
                    return redirect('/admin/changepwd').'两次输入的新密码不一致';
                }
            }else{
                    return redirect('/admin/changepwd').'旧密码不正确';
            }

        }

        //添加分公司()
        public function add_companyin(Request $row)
        {
            //判定总经理与副经理不能为同一人
            if($row -> company_manager == $row -> company_managermin)
            {
                return redirect('/admin/add_company');
            }
            $data = array(
                'company' => $row -> company,
                'company_place' => $row -> company_place,
                'company_level' => $row -> company_level,
                'company_status' => $row -> company_status,
                'company_manager' => $row -> company_manager,
                'company_managermin' => $row -> company_managermin,
            );
            $admin = array(
                'admin_post' => '总经理'
            );
            $adminn = array(
                'admin_post' => '副经理'
            );
            //修改职位
            DB::table('adminuser') -> where('admin_truename',$row -> company_manager) -> update($admin);
            DB::table('adminuser') -> where('admin_truename',$row -> company_managermin) -> update($adminn);
            //添进公司
            DB::table('company') -> insert($data);
            return redirect('/admin/company');
        }








        //测试函数
        public function domain(){
           DB::transaction(function() {
                 $user = DB::table('adminuser')->where('id', 69)->lockForUpdate()->first();
                 // dd($user);
               });
        }



}
